﻿//========================================================================//
//      Copyright 2012, Smilodon Studio Inc.
//      All rights reserved.
//========================================================================//

/**
 *	base 
 *      @author: CYS
 */


var canvas;
var g2D;

var height;
var width;
var fullWindow = true;
var ratio = 1;

var time = 0;
var dtime = 0;
var startime;
var lasttime;
var g_infoShow = new infoShow();
var bClear = false;
var clearTime = 0;
var bMouseDown = false;

var lineColor = 'rgba(125, 200, 225, 1)';
var maxNumBalls = 20;
var numLiveBalls = 20;
var balls = [];

function canvasResize()
{
	ratio = window.innerWidth/canvas.width;
	canvas.width = window.innerWidth;
	canvas.height = window.innerHeight;
	width = canvas.width;
	height = canvas.height;
	if(!g2D)g2D = canvas.getContext("2d");
	g2D.lineWidth = 2;
	g2D.lineCap = 'round';
	g2D.strokeStyle = lineColor;
	g2D.fillStyle   = lineColor;

}

function init() 
{
	canvas = document.getElementById('canvas2D');
	canvasResize();
	
	var touchable = 'createTouch' in document;
	if (touchable)
	{
		canvas.addEventListener('touchstart', onTouchStart, false);
		canvas.addEventListener('touchmove', onTouchMove, false);
		canvas.addEventListener('touchend', onTouchEnd, false);
	}
	else
	{
		canvas.addEventListener('mousedown', onTouchStart, false);
		canvas.addEventListener('mousemove', onTouchMove, false);
		canvas.addEventListener('mouseup', onTouchEnd, false);
	}

	canvas.addEventListener( 'contextmenu', function ( event ) { event.preventDefault(); }, false );
	
	for(var i = 0; i < maxNumBalls; i++)
	{
		var b = new ball();
		b.reBegin(width, height);
		balls.push(b);
	}
}

var bTimeout = false;
window.onresize = function()
{
	if (fullWindow) canvasResize();
	bTimeout = true;
}

var first = true;
var lastOrien;

window.ondeviceorientation =  function(e) 
{	
	if(first)
	{
		lastOrien = window.orientation;
		first = false;
	}
	if(window.orientation !== lastOrien)
	{
		window.onresize();
		lastOrien = window.orientation;
	}
	
	g_infoShow.add("alpa: ", e.alpha);
	g_infoShow.add("beta: ",e.beta);
	g_infoShow.add("gamma: ",e.gamma);
	g_infoShow.add("orientation: ",window.orientation);
}

window.onblur = function()
{
	bTimeout = true;
}

function drawScene()
{
	// draw background color 
	g2D.clearRect(0, 0, canvas.width,canvas.height);
	
	g2D.save();
	
	for(var i = 0; i < numLiveBalls; i++) 
	{
		var b = balls[i];
		b.go(dtime*0.051);
		if(b.x > 2*width) b.reBegin(width, height);
		if(b.life > 0) b.draw();
	}
	
	g2D.restore();
	
	//g_infoShow.show();

}



function onTouchStart(event)
{
	// Prevent the browser from doing its default thing (scroll, zoom)
	event.preventDefault();


	if(event.clientX) // on mouse down
	{
		var mx = event.clientX;
		var my = event.clientY;
		for(var i = 0; i < numLiveBalls; i++) 
		{
			var b = balls[i];
			if(b.isCol(mx, my)) b.reBegin(width, height);
		}
		bMouseDown = true;
	}
	
	if(!event.touches) return;
	// on touch star
	for(var i = 0; i < event.touches.length; i++)
	{
		var mx = event.touches[i].clientX;
		var my = event.touches[i].clientY;
		for(var j = 0; j < numLiveBalls; j++) 
		{
			var b = balls[j];
			if(b.isCol(mx, my)) b.reBegin(width, height);
		}	
	}

}

function onTouchMove(event)
{
	event.preventDefault();

	if(event.clientX && bMouseDown) // on mouse move
	{
		
	}
	
	if(!event.touches) return;
	
	for(var i = 0; i < event.touches.length; i++)
	{

	}
}

function onTouchEnd(event)
{
	event.preventDefault();

	if(event.clientX) // on mouse up
	{
		bMouseDown = false;
	}
	if(!event.touches) return;
	
	for(var i = 0; i < event.touches.length; i++)
	{
	
	}
	
}

function gameloop() 
{
	window.requestAnimFrame( gameloop );
	if(bTimeout)
	{
		lasttime = Date.now();
		bTimeout = false;
	}
	dtime = Date.now() - lasttime;
	time = Date.now() - startime;
	drawScene();
	lasttime = Date.now();
}


function game()
{
	init();
	gameloop();
}




